package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.common.CsmcConstant;
import cc.rengu.redp.bizimpl.csmc.domain.CAcctEntry;
import cc.rengu.redp.bizimpl.csmc.dao.CAcctEntryMapper;
import cc.rengu.redp.bizimpl.csmc.domain.CInstructionRule;
import cc.rengu.redp.bizimpl.csmc.domain.CSubjectInfo;
import cc.rengu.redp.bizimpl.csmc.service.ICAcctEntryService;
import cc.rengu.redp.bizimpl.csmc.service.ICSubjectInfoService;
import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BizTmp;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lizk
 */
@Service
@Slf4j
@DS("csmc")
public class CAcctEntryServiceImpl extends ServiceImpl<CAcctEntryMapper, CAcctEntry> implements ICAcctEntryService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;
    @Autowired
    private IUpmpSysSeqService sysSeqService;
    @Autowired
    private ICSubjectInfoService subjectInfoService;

    private long oprMenuId = 1;
    private String oprMenuName = "清分清算参数管理:会计分录配置";
    private long chkMenuId = 2;
    private String chkMenuName = "清分清算参数管理:会计分录配置";
    private String webNextUrl = "/admin/csmc/acctEntry";


    @Override
    public Map<String, Object> findByPage(QueryRequest queryRequest, CAcctEntry acctEntry) {

        Map<String, Object> result = new HashMap<>();
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        try {
            String instId = acctEntry.getInstId();
            if (Common.isBlank(instId)) {
                instId = user.getInstId();
            }
            QueryWrapper<CAcctEntry> queryWrapper = new QueryWrapper<>();


            if (!StringUtils.isEmpty(instId)) {
                queryWrapper.lambda().eq(CAcctEntry::getInstId, instId);
            }
            if (!StringUtils.isEmpty(acctEntry.getAcctEntryDesc())) {
                queryWrapper.lambda().like(CAcctEntry::getAcctEntryDesc, acctEntry.getAcctEntryDesc());
            }

            List<CAcctEntry> list = baseMapper.selectList(queryWrapper);


            list = list.stream().filter(CommonUtils.distinctByKey(b -> b.getAcctEntryId())).collect(Collectors.toList());

            for (CAcctEntry entry : list) {
                entry.setId(entry.getAcctEntryId().substring(2));
            }

            result.put("rows", list);
            result.put("total", CollectionUtils.isEmpty(list) ? 0 : list.size());

            return result;
        } catch (Exception e) {
            log.error("获取会计分录信息失败", e);
            result.put("rows", null);
            result.put("total", 0);
            return result;
        }
    }

    @Override
    public void createAcctEntry(List<CAcctEntry> acctEntryList, String optFlag) throws RedpException {
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(optFlag)) {
            for (CAcctEntry acctEntry : acctEntryList) {
                acctEntry.setAcctEntryId(generateCode());
            }
        }
        if (optFlag.equals("chk") || "stop".equals(optFlag) || "use".equals(optFlag) || optFlag.equals("upd")) {
            if (Common.isBlank(acctEntryList.get(0).getAcctEntryId())) {
                log.error("请求参数(AcctEntryId)异常!");
                throw new RedpException("请求参数(AcctEntryId)异常!");
            }
            LambdaQueryWrapper<CAcctEntry> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CAcctEntry::getAcctEntryId, acctEntryList.get(0).getAcctEntryId());
            List<CAcctEntry> entryList = baseMapper.selectList(queryWrapper);
            if (Common.isBlank(entryList.get(0).getId()) || entryList.size() <= 0) {
                log.error("根据id没有找到指定数据！");
                throw new RedpException("根据id没有找到指定数据！");
            }
        }
        saveData(acctEntryList, optFlag);
    }


    @Override
    public Map<String, Object> findAllAcctEntryIdByOprStatus(String oprStatus, String q, QueryRequest request) {

        if ("null".equals(q) || Common.isBlank(q)) {
            q = "";
        }

        try {
            QueryWrapper<CAcctEntry> wrapper = new QueryWrapper<>();
            wrapper.select("Distinct ACCT_ENTRY_ID", "ACCT_ENTRY_DESC");

            wrapper.lambda().eq(CAcctEntry::getOprStatus, oprStatus);

            if (org.apache.commons.lang3.StringUtils.isNotEmpty(q)) {
                String acctEntryIdOrDesc = q;
                wrapper.lambda().and(i -> i.like(CAcctEntry::getAcctEntryId, acctEntryIdOrDesc).or().like(CAcctEntry::getAcctEntryDesc, acctEntryIdOrDesc));
            }

            Page<CAcctEntry> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<CAcctEntry> page1 = this.page(page, wrapper);
            List<CAcctEntry> listx = page1.getRecords();
            Map<String, Object> maps = new HashMap<String, Object>();
            Map<String, String> map = new HashMap<String, String>();
            List<Object> list = new ArrayList<Object>();
            if (listx != null) {
                for (int i = 0; i < listx.size(); i++) {
                    CAcctEntry org = listx.get(i);
                    map = new HashMap<String, String>();
                    map.put("id", org.getAcctEntryId());
                    map.put("name", org.getAcctEntryDesc());
                    list.add(map);
                }
                maps.put("results", list);
                maps.put("total", page.getTotal());
                int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
                if (pageIndex == page1.getTotal()) {
                    maps.put("more", false);
                } else {
                    maps.put("more", true);
                }
            }
            return maps;

        } catch (Exception e) {
            log.error("获取会计分录标识信息失败", e);
            return null;
        }
    }

    @Override
    public Map<String, Object> searchAcctEntryData(String id, String optFlag, String acctEntryId, String acctEntryDesc) throws RedpException {

        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(id)) {
            log.warn("请求参数(id)异常!");
            result.put("rows", null);
            result.put("total", 0);
            return result;
        }
        try {

            List<CAcctEntry> list = searchAcctEntryList(id, optFlag, acctEntryId, acctEntryDesc);
            result.put("rows", list);
            result.put("total", CollectionUtils.isEmpty(list) ? 0 : list.size());
        } catch (Exception e) {
            log.error("获取会计分录信息列表失败", e);
            result.put("rows", null);
            result.put("total", 0);
            throw new RedpException("获取会计分录信息列表失败");
        }
        return result;
    }

    public List<CAcctEntry> searchAcctEntryList(String id, String optFlag, String acctEntryId, String acctEntryDesc) throws RedpException {

        List<CAcctEntry> acctEntryList = new ArrayList<>();
        List<CAcctEntry> list = new ArrayList<>();
        List<CAcctEntry> acctEntryListD = new ArrayList<>();
        List<CAcctEntry> acctEntryListC = new ArrayList<>();
        if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(optFlag)) {
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                throw new RedpException("请求参数(id)异常！");
            }
            // 审核：获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getBizTmpDetail(id);
            if (Common.isBlank(tmp)) {
                log.error("根据id没有找到指定临时表数据！");
                throw new RedpException("根据id没有找到指定临时表数据！");
            }
            String bizdata = "";
            if (null != tmp.getBizData2()) {
                bizdata += tmp.getBizData2();
            }
            if (null != tmp.getBizData3()) {
                bizdata += tmp.getBizData3();
            }

            CAcctEntry acctEntry1 = JSON.parseObject(bizdata, CAcctEntry.class);
            List<BizTmp> bizTmpListByUniqueKey = getBizTmpListByUniqueKey(acctEntry1.getInstId(), acctEntry1.getClass().getAnnotation(TableName.class).value().toLowerCase(), tmp.getBizData1());

            for (BizTmp biztmp : bizTmpListByUniqueKey) {
                String bizdata1 = "";
                if (null != biztmp.getBizData2()) {
                    bizdata1 += biztmp.getBizData2();
                }
                if (null != biztmp.getBizData3()) {
                    bizdata1 += biztmp.getBizData3();
                }
                CAcctEntry acctEntry = JSON.parseObject(bizdata1, CAcctEntry.class);
                acctEntryList.add(acctEntry);
            }

            if (Common.isBlank(acctEntryList) || acctEntryList.size() <= 0) {
                log.error("根据id没有找到指定数据！");
                throw new RedpException("根据id没有找到指定数据！");
            }
            if (Common.isNotBlank(acctEntryList) && acctEntryList.size() > 0) {
                List<CSubjectInfo> subjectList = subjectInfoService.list();
                Map<String, String> subjectIdDescMap = new HashMap<>();

                for (CSubjectInfo subjectInfo : subjectList) {
                    subjectIdDescMap.put(subjectInfo.getSettleAcctId(), subjectInfo.getSettleAcctDesc());
                }
                for (CAcctEntry acctEntry : acctEntryList) {
                    if (acctEntry.getCreditDebitFlag().equals(CsmcConstant.DEBIT_FLAG)) {
                        acctEntryListD.add(acctEntry);
                    }
                    if (acctEntry.getCreditDebitFlag().equals(CsmcConstant.CREDIT_FLAG)) {
                        acctEntryListC.add(acctEntry);
                    }
                }
                for (CAcctEntry acctEntryD : acctEntryListD) {
                    CAcctEntry acctentry = new CAcctEntry();
                    acctentry.setSettleAcctDescD(subjectIdDescMap.get(acctEntryD.getSettleAcctId()));
                    acctentry.setSettleRoleD(acctEntryD.getSettleRole());
                    acctentry.setSettleAcctIdD(acctEntryD.getSettleAcctId());
                    acctentry.setAmountDirectionD(acctEntryD.getAmountDirection());
                    acctentry.setGroupNo(acctEntryD.getGroupNo());
                    acctentry.setDId(acctEntryD.getId());
                    list.add(acctentry);
                }

                for (int i = 0; i < acctEntryListC.size(); i++) {
                    for (CAcctEntry acctentry : list) {
                        acctentry.setSettleAcctDescC(subjectIdDescMap.get(acctEntryListC.get(i).getSettleAcctId()));
                        acctentry.setSettleRoleC(acctEntryListC.get(i).getSettleRole());
                        acctentry.setSettleAcctIdC(acctEntryListC.get(i).getSettleAcctId());
                        acctentry.setAmountDirectionC(acctEntryListC.get(i).getAmountDirection());
                        acctentry.setCId(acctEntryListC.get(i).getId());
                    }
                }
            }
        } else if (!"0".equals(id)) {
            LambdaQueryWrapper<CAcctEntry> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CAcctEntry::getAcctEntryId, acctEntryId)
                    .eq(CAcctEntry::getAcctEntryDesc, acctEntryDesc);

            acctEntryList = baseMapper.selectList(wrapper);

            if (Common.isNotBlank(acctEntryList) && acctEntryList.size() > 0) {
                List<CSubjectInfo> subjectList = subjectInfoService.list();
                Map<String, String> subjectIdDescMap = new HashMap<>();

                for (CSubjectInfo subjectInfo : subjectList) {
                    subjectIdDescMap.put(subjectInfo.getSettleAcctId(), subjectInfo.getSettleAcctDesc());
                }
                for (CAcctEntry acctEntry : acctEntryList) {

                    if (acctEntry.getCreditDebitFlag().equals(CsmcConstant.DEBIT_FLAG)) {
                        acctEntryListD.add(acctEntry);
                    }
                    if (acctEntry.getCreditDebitFlag().equals(CsmcConstant.CREDIT_FLAG)) {
                        acctEntryListC.add(acctEntry);
                    }
                }

                for (CAcctEntry acctEntryD : acctEntryListD) {
                    CAcctEntry acctentry = new CAcctEntry();
                    acctentry.setSettleAcctDescD(subjectIdDescMap.get(acctEntryD.getSettleAcctId()));
                    acctentry.setSettleRoleD(acctEntryD.getSettleRole());
                    acctentry.setSettleAcctIdD(acctEntryD.getSettleAcctId());
                    acctentry.setAmountDirectionD(acctEntryD.getAmountDirection());
                    acctentry.setGroupNo(acctEntryD.getGroupNo());
                    acctentry.setDId(acctEntryD.getId());
                    list.add(acctentry);
                }

                for (CAcctEntry acctEntryC : acctEntryListC) {
                    if(list.size() == 1 && list.get(0).getGroupNo() == acctEntryC.getGroupNo()) {
                        list.get(0).setSettleAcctDescC(subjectIdDescMap.get(acctEntryC.getSettleAcctId()));
                        list.get(0).setSettleRoleC(acctEntryC.getSettleRole());
                        list.get(0).setSettleAcctIdC(acctEntryC.getSettleAcctId());
                        list.get(0).setAmountDirectionC(acctEntryC.getAmountDirection());
                        list.get(0).setCId(acctEntryC.getId());
                    }else {
                        CAcctEntry acctentryc = new CAcctEntry();
                        acctentryc.setSettleAcctDescC(subjectIdDescMap.get(acctEntryC.getSettleAcctId()));
                        acctentryc.setSettleRoleC(acctEntryC.getSettleRole());
                        acctentryc.setSettleAcctIdC(acctEntryC.getSettleAcctId());
                        acctentryc.setAmountDirectionC(acctEntryC.getAmountDirection());
                        acctentryc.setGroupNo(acctEntryC.getGroupNo());
                        acctentryc.setCId(acctEntryC.getId());
                        list.add(acctentryc);
                    }
                }
            }
        }
        return list;
    }

    private void saveData(List<CAcctEntry> acctEntryList, String flag) throws RedpException {

        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            List<CAcctEntry> list = new ArrayList<>();
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(acctEntryList.get(0).getInstId(), "acctEntry:add");
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                //保存数据到正式表审核状态为新增待审核数据状态为非正式
                for (int idx = 0; idx < acctEntryList.size(); idx++) {
                    CAcctEntry acctentry = acctEntryList.get(idx);
                    CAcctEntry acctentryD = new CAcctEntry();
                    CAcctEntry acctentryC = new CAcctEntry();

                    acctentryD.setId(null);
                    acctentryD.setInstId(acctentry.getInstId());
                    acctentryD.setOprId(user.getId());
                    acctentryD.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setAuditOprId(user.getId());
                    acctentryD.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    acctentryD.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    acctentryD.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryD.setGroupNo(acctentry.getGroupNo());
                    acctentryD.setSettleRole(acctentry.getSettleRoleD());
                    acctentryD.setAmountDirection(acctentry.getAmountDirectionD());
                    acctentryD.setCreditDebitFlag(CsmcConstant.DEBIT_FLAG);
                    acctentryD.setSettleAcctId(acctentry.getSettleAcctIdD());
                    acctentryD.setSettleAcctDesc(acctentry.getSettleAcctDescD());
                    acctentryD.setAcctEntryDesc(acctentry.getAcctEntryDesc());

                    acctentryC.setId(null);
                    acctentryC.setInstId(acctentry.getInstId());
                    acctentryC.setOprId(user.getId());
                    acctentryC.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setAuditOprId(user.getId());
                    acctentryC.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    acctentryC.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    acctentryC.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryC.setGroupNo(acctentry.getGroupNo());
                    acctentryC.setSettleRole(acctentry.getSettleRoleC());
                    acctentryC.setAmountDirection(acctentry.getAmountDirectionC());
                    acctentryC.setCreditDebitFlag(CsmcConstant.CREDIT_FLAG);
                    acctentryC.setSettleAcctId(acctentry.getSettleAcctIdC());
                    acctentryC.setSettleAcctDesc(acctentry.getSettleAcctDescC());
                    acctentryC.setAcctEntryDesc(acctentry.getAcctEntryDesc());

                    list.add(acctentryD);
                    list.add(acctentryC);
                }
                this.mergeData(list, acctEntryList.get(0).getInstId());
                // 保存数据到临时表，数据状态为非正式，审核为新增待审核
                int i = 0;
                for (CAcctEntry acctentry : list) {
                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(acctentry);
                    if (bizData.length() > 8000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    String ename = acctentry.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = acctentry.getClass().getAnnotation(Description.class).value();
                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData2 = bizData.substring(0, 3999);
                        bizData3 = bizData.substring(4000);
                    } else {
                        bizData2 = bizData;
                    }
                    if (bizData3.length() > 4000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    tmp.setBizData1(acctentry.getAcctEntryId());
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setBizOpr(BizOprEnum.OPR_ADD.getType());
                    tmp.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    tmp.setLastOprId(user.getId());
                    tmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(oprMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                    tmp.setInstId(acctentry.getInstId());
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    tmp.setRealId(acctentry.getId());
                    tmp.setWebNextUrl(webNextUrl);
                    /*只保存一条为主记录，审核列表界面只展示一条*/
                    if (i != 0) {
                        tmp.setDetailFlag("Y");
                    }
                    i++;
                    bizTmpService.getBaseMapper().insert(tmp);
                }

            } else {
                for (int idx = 0; idx < acctEntryList.size(); idx++) {
                    CAcctEntry acctentry = acctEntryList.get(idx);
                    CAcctEntry acctentryD = new CAcctEntry();
                    CAcctEntry acctentryC = new CAcctEntry();

                    acctentryD.setId(acctEntryList.get(idx).getDId());
                    acctentryD.setInstId(acctentry.getInstId());
                    acctentryD.setOprId(user.getId());
                    acctentryD.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setAuditOprId(user.getId());
                    acctentryD.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    acctentryD.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    acctentryD.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryD.setGroupNo(acctentry.getGroupNo());
                    acctentryD.setSettleRole(acctentry.getSettleRoleD());
                    acctentryD.setAmountDirection(acctentry.getAmountDirectionD());
                    acctentryD.setCreditDebitFlag(CsmcConstant.DEBIT_FLAG);
                    acctentryD.setSettleAcctId(acctentry.getSettleAcctIdD());
                    acctentryD.setSettleAcctDesc(acctentry.getSettleAcctDescD());
                    acctentryD.setAcctEntryDesc(acctentry.getAcctEntryDesc());

                    acctentryC.setId(acctEntryList.get(idx).getCId());
                    acctentryC.setInstId(acctentry.getInstId());
                    acctentryC.setOprId(user.getId());
                    acctentryC.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setAuditOprId(user.getId());
                    acctentryC.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    acctentryC.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    acctentryC.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryC.setGroupNo(acctentry.getGroupNo());
                    acctentryC.setSettleRole(acctentry.getSettleRoleC());
                    acctentryC.setAmountDirection(acctentry.getAmountDirectionC());
                    acctentryC.setCreditDebitFlag(CsmcConstant.CREDIT_FLAG);
                    acctentryC.setSettleAcctId(acctentry.getSettleAcctIdC());
                    acctentryC.setSettleAcctDesc(acctentry.getSettleAcctDescC());
                    acctentryC.setAcctEntryDesc(acctentry.getAcctEntryDesc());

                    list.add(acctentryD);
                    list.add(acctentryC);
                }
                this.mergeData(list, acctEntryList.get(0).getInstId());
                // 保存历史表
                for (CAcctEntry acctentry : list) {
                    bizHisService.saveBizHisNew(acctentry, acctentry, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(),
                            lastUpddt, String.valueOf(oprMenuId), oprMenuName, acctentry.getId(), null, null, null);

                }
            }
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            List<CAcctEntry> acctentrylist = new ArrayList<>();
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(acctEntryList.get(0).getInstId(), "acctEntry:edit");
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                // 修改正式表状态
                upstatus(acctEntryList.get(0).getAcctEntryId(), BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType(), acctEntryList.get(0).getOprStatus(), user.getId(), DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                for (int idx = 0; idx < acctEntryList.size(); idx++) {
                    CAcctEntry acctentry = acctEntryList.get(idx);
                    CAcctEntry acctentryD = new CAcctEntry();
                    CAcctEntry acctentryC = new CAcctEntry();


                    acctentryD.setInstId(acctEntryList.get(idx).getInstId());
                    acctentryD.setOprId(acctEntryList.get(idx).getOprId());
                    acctentryD.setCreateTime(acctEntryList.get(idx).getCreateTime());
                    acctentryD.setAuditOprId(user.getId());
                    acctentryD.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    acctentryD.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                    acctentryD.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryD.setGroupNo(acctentry.getGroupNo());
                    acctentryD.setSettleRole(acctentry.getSettleRoleD());
                    acctentryD.setAmountDirection(acctentry.getAmountDirectionD());
                    acctentryD.setCreditDebitFlag(CsmcConstant.DEBIT_FLAG);
                    acctentryD.setSettleAcctId(acctentry.getSettleAcctIdD());
                    acctentryD.setSettleAcctDesc(acctentry.getSettleAcctDescD());
                    acctentryD.setAcctEntryDesc(acctentry.getAcctEntryDesc());
                    acctentryD.setId(acctentry.getDId());

                    acctentryC.setInstId(acctentry.getInstId());
                    acctentryC.setOprId(user.getId());
                    acctentryC.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setAuditOprId(user.getId());
                    acctentryC.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    acctentryC.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                    acctentryC.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryC.setGroupNo(acctentry.getGroupNo());
                    acctentryC.setSettleRole(acctentry.getSettleRoleC());
                    acctentryC.setAmountDirection(acctentry.getAmountDirectionC());
                    acctentryC.setCreditDebitFlag(CsmcConstant.CREDIT_FLAG);
                    acctentryC.setSettleAcctId(acctentry.getSettleAcctIdC());
                    acctentryC.setSettleAcctDesc(acctentry.getSettleAcctDescC());
                    acctentryC.setAcctEntryDesc(acctentry.getAcctEntryDesc());
                    acctentryC.setId(acctentry.getCId());

                    acctentrylist.add(acctentryD);
                    acctentrylist.add(acctentryC);
                }

                // 将修改数据同步到临时表
                for (CAcctEntry acctentry : acctentrylist) {
                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(acctentry);
                    if (bizData.length() > 8000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    String ename = acctentry.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = acctentry.getClass().getAnnotation(Description.class).value();
                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData2 = bizData.substring(0, 3999);
                        bizData3 = bizData.substring(4000);
                    } else {
                        bizData2 = bizData;
                    }
                    if (bizData3.length() > 4000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    tmp.setBizData1(acctentry.getAcctEntryId());
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setBizOpr(BizOprEnum.OPR_UPDATE.getType());
                    tmp.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    tmp.setLastOprId(user.getId());
                    tmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(oprMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                    tmp.setInstId(acctentry.getInstId());
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                    tmp.setRealId(acctentry.getId());
                    tmp.setWebNextUrl(webNextUrl);

                    //新增、修改、删除临时表记录
                    BizTmpData(tmp);
                }

            } else {
                //正式表数据
                List<CAcctEntry> acctentryList = findacctEntryId(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId());
                //将数据保存到正式表 审核状态为修改待审核数据状态为正式
                for (int idx = 0; idx < acctEntryList.size(); idx++) {
                    CAcctEntry acctentry = acctEntryList.get(idx);
                    CAcctEntry acctentryD = new CAcctEntry();
                    CAcctEntry acctentryC = new CAcctEntry();


                    acctentryD.setInstId(acctEntryList.get(idx).getInstId());
                    acctentryD.setOprId(acctEntryList.get(idx).getOprId());
                    acctentryD.setCreateTime(acctEntryList.get(idx).getCreateTime());
                    acctentryD.setAuditOprId(user.getId());
                    acctentryD.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryD.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    acctentryD.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    acctentryD.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryD.setGroupNo(acctentry.getGroupNo());
                    acctentryD.setSettleRole(acctentry.getSettleRoleD());
                    acctentryD.setAmountDirection(acctentry.getAmountDirectionD());
                    acctentryD.setCreditDebitFlag(CsmcConstant.DEBIT_FLAG);
                    acctentryD.setSettleAcctId(acctentry.getSettleAcctIdD());
                    acctentryD.setSettleAcctDesc(acctentry.getSettleAcctDescD());
                    acctentryD.setAcctEntryDesc(acctentry.getAcctEntryDesc());
                    acctentryD.setId(acctentry.getDId());

                    acctentryC.setInstId(acctentry.getInstId());
                    acctentryC.setOprId(user.getId());
                    acctentryC.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setAuditOprId(user.getId());
                    acctentryC.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    acctentryC.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    acctentryC.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    acctentryC.setAcctEntryId(acctentry.getAcctEntryId());
                    acctentryC.setGroupNo(acctentry.getGroupNo());
                    acctentryC.setSettleRole(acctentry.getSettleRoleC());
                    acctentryC.setAmountDirection(acctentry.getAmountDirectionC());
                    acctentryC.setCreditDebitFlag(CsmcConstant.CREDIT_FLAG);
                    acctentryC.setSettleAcctId(acctentry.getSettleAcctIdC());
                    acctentryC.setSettleAcctDesc(acctentry.getSettleAcctDescC());
                    acctentryC.setAcctEntryDesc(acctentry.getAcctEntryDesc());
                    acctentryC.setId(acctentry.getCId());

                    acctentrylist.add(acctentryD);
                    acctentrylist.add(acctentryC);
                }

                this.mergeData(acctentrylist, acctEntryList.get(0).getInstId());

                //保存历史数据
                BizHisData(acctentrylist, acctentryList, user, user.getInstId(), String.valueOf(oprMenuId), oprMenuName, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));


            }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {

            String ename = acctEntryList.get(0).getClass().getAnnotation(TableName.class).value().toLowerCase();
            //历史表记录
            List<BizTmp> bizTmpList = getBizTmpListByUniqueKey(acctEntryList.get(0).getInstId(), ename, acctEntryList.get(0).getAcctEntryId());
            //正式表记录
            List<CAcctEntry> acctentrylist = findacctEntryId(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId());

            List entitys = new ArrayList<>();
            List entityOlds = acctentrylist;
            List bizOpr = new ArrayList<>();
            List realIds = new ArrayList<>();
            String bizData2 = "";
            String bizData3 = "";
            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(acctEntryList.get(0).getInstId(), "会计分录配置:停用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表审核状态为停用审批流程中
                    stopupdate(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType(), acctEntryList.get(0).getOprStatus(), user.getId(), DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), acctEntryList.get(0).getStopReason());
                    //修改临时表数据
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        acctentry.setStopReason(acctEntryList.get(0).getStopReason());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctEntryList.get(0).getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());

                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                } else {
                    //修改正式表数据状态
                    //stopupdate(acctEntryList.get(0).getAcctEntryId(), acctentry.getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType(), BizDataStatusEnum.STATUS_STOPED.getType(), user.getId(), lastUpddt, acctEntryList.get(0).getStopReason());
                    for (CAcctEntry acctentry : acctentrylist) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        acctentry.setAuditOprId(user.getId());
                        acctentry.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        acctentry.setStopReason(acctEntryList.get(0).getStopReason());
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctentrylist);
                    entitys = acctentrylist;

                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(acctEntryList.get(0).getInstId(), "会计分录配置:启用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表审核状态为启用审批流程中
                    useupdate(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType(), acctEntryList.get(0).getOprStatus(), user.getId(), DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    //修改临时表数据
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctEntryList.get(0).getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());

                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }


                } else {
                    //修改正式表数据状态
                    //useupdate(acctEntryList.get(0).getAcctEntryId(), acctentry.getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType(), BizDataStatusEnum.STATUS_NORMAL.getType(), user.getId(), lastUpddt);
                    for (CAcctEntry acctentry : acctentrylist) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        acctentry.setAuditOprId(user.getId());
                        acctentry.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        acctentry.setStopReason(acctEntryList.get(0).getStopReason());
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctentrylist);
                    entitys = acctentrylist;
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(acctEntryList.get(0).getInstId(), "会计分录配置:注销");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    //修改正式表审核状态为注销审批流程中
                    cancelupdate(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType(), acctEntryList.get(0).getOprStatus(), user.getId(), DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), acctEntryList.get(0).getCancelReason());
                    //修改临时表数据
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        acctentry.setCancelReason(acctEntryList.get(0).getCancelReason());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctEntryList.get(0).getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                } else {
                    //修改正式表数据状态
                    //cancelupdate(acctEntryList.get(0).getAcctEntryId(), acctentry.getInstId(), BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType(), BizDataStatusEnum.STATUS_CANCELED.getType(), user.getId(), lastUpddt, acctEntryList.get(0).getCancelReason());
                    for (CAcctEntry acctentry : acctentrylist) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        acctentry.setAuditOprId(user.getId());
                        acctentry.setUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        acctentry.setStopReason(acctEntryList.get(0).getCancelReason());
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctentrylist);
                    entitys = acctentrylist;
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                }
            }
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {

            List<CAcctEntry> acctEntryListOlds = findacctEntryId(acctEntryList.get(0).getAcctEntryId(), acctEntryList.get(0).getInstId());
            List<CAcctEntry> acctEntryLists = new ArrayList();
            String ename = acctEntryList.get(0).getClass().getAnnotation(TableName.class).value().toLowerCase();
            //获取临时表数据并转换成实体对象
            List<BizTmp> bizTmpList = getBizTmpListByUniqueKey(acctEntryList.get(0).getInstId(), ename, acctEntryList.get(0).getAcctEntryId());
            if (Common.isBlank(bizTmpList)) {
                log.error("根据id没有找到指定临时表数据！");
                throw new RedpException("根据id没有找到指定临时表数据！");
            }
            for (BizTmp biztmp : bizTmpList) {
                String bizdata = "";
                if (null != biztmp.getBizData2()) {
                    bizdata += biztmp.getBizData2();
                }
                if (null != biztmp.getBizData3()) {
                    bizdata += biztmp.getBizData3();
                }
                CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                if (Common.isBlank(acctentry.getId())) {
                    acctentry.setId(biztmp.getRealId());
                }
                acctEntryLists.add(acctentry);
            }

            String opt = acctEntryList.get(0).getOpt();
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                throw new RedpException("请求参数(opt)异常!");
            }
            String impStatus = "";
            String bizData2 = "";
            String bizData3 = "";
            List entitys = new ArrayList<>();
            List entityOlds = new ArrayList<>();
            List bizOpr = new ArrayList<>();
            List realIds = new ArrayList<>();
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(acctEntryList.get(0).getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为新增审核通过数据状态为正式
                    for (CAcctEntry acctentry : acctEntryLists) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        bizOpr.add(BizOprEnum.OPR_ADD.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctEntryLists);
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctentry.getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                    entitys = acctEntryLists;
                    entityOlds = acctEntryListOlds;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                } else {
                    //修改正式表审核状态为新增审批拒绝数据状态为非正式
                    for (CAcctEntry acctentry : acctEntryListOlds) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        acctentry.setAuditRefuseReason(acctEntryList.get(0).getAuditRefuseReason());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    }
                    this.updateBatchById(acctEntryListOlds);
                    //修改临时表审核状态为新增审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(acctEntryList.get(0).getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为注销审核通过数据状态为正式
                    for (CAcctEntry acctentry : acctEntryLists) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctEntryLists);
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctentry.getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                    entitys = acctEntryLists;
                    entityOlds = acctEntryListOlds;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, acctEntryList.get(0).getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                } else {
                    //修改正式表审核状态为注销审批拒绝
                    for (CAcctEntry acctentry : acctEntryListOlds) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                        acctentry.setOprStatus(acctEntryList.get(0).getOprStatus());
                        acctentry.setAuditRefuseReason(acctEntryList.get(0).getAuditRefuseReason());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    }
                    this.updateBatchById(acctEntryListOlds);
                    //修改临时表审核状态为新增审批拒绝
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(acctEntryList.get(0).getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为注销审核通过数据状态为正式
                    for (CAcctEntry acctentry : acctEntryLists) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctEntryLists);
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctentry.getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                    entitys = acctEntryLists;
                    entityOlds = acctEntryListOlds;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, acctEntryList.get(0).getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                } else {
                    //修改正式表审核状态为停用审批拒绝
                    for (CAcctEntry acctentry : acctEntryListOlds) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                        acctentry.setOprStatus(acctEntryList.get(0).getOprStatus());
                        acctentry.setAuditRefuseReason(acctEntryList.get(0).getAuditRefuseReason());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    }
                    this.updateBatchById(acctEntryListOlds);
                    //修改临时表审核状态为新增审批拒绝
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(acctEntryList.get(0).getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //修改正式表审核状态为启用审核通过数据状态为正式
                    for (CAcctEntry acctentry : acctEntryLists) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        realIds.add(acctentry.getId());
                    }
                    this.updateBatchById(acctEntryLists);
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctentry.getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }
                    entitys = acctEntryLists;
                    entityOlds = acctEntryListOlds;
                    //保存数据到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, acctEntryList.get(0).getInstId(), bizOpr, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), String.valueOf(oprMenuId), oprMenuName, realIds, null, null, null);

                } else {
                    //修改正式表审核状态为停用审批拒绝
                    for (CAcctEntry acctentry : acctEntryListOlds) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                        acctentry.setOprStatus(acctEntryList.get(0).getOprStatus());
                        acctentry.setAuditRefuseReason(acctEntryList.get(0).getAuditRefuseReason());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    }
                    this.updateBatchById(acctEntryListOlds);
                    //修改临时表审核状态为新增审批拒绝
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(acctEntryList.get(0).getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    //将临时表数据同步到正式表
                    for (CAcctEntry acctentry : acctEntryLists) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        realIds.add(acctentry.getId());
                    }
                    mergeData(acctEntryLists, acctEntryList.get(0).getInstId());
                    //修改临时表数据审核状态为修改审批通过，数据状态为正式
                    for (BizTmp biztmp : bizTmpList) {
                        String bizdata = "";
                        if (null != biztmp.getBizData2()) {
                            bizdata += biztmp.getBizData2();
                        }
                        if (null != biztmp.getBizData3()) {
                            bizdata += biztmp.getBizData3();
                        }
                        CAcctEntry acctentry = JSON.parseObject(bizdata, CAcctEntry.class);
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        acctentry.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        String bizData = JSON.toJSONString(acctentry);
                        if (bizData.length() > 4000) {
                            bizData2 = bizData.substring(0, 3999);
                            bizData3 = bizData.substring(4000);
                        } else {
                            bizData2 = bizData;
                        }
                        if (bizData3.length() > 4000) {
                            throw new RedpException("业务数据过长，请联系管理员！");
                        }
                        biztmp.setBizData1(acctentry.getAcctEntryId());
                        biztmp.setBizData2(bizData2);
                        biztmp.setBizData3(bizData3);
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                    //将正式表数据同步到历史表

                    BizHisData(acctEntryLists, acctEntryListOlds, user, user.getInstId(), String.valueOf(chkMenuId), chkMenuName, DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                } else {
                    //修改正式表审核状态为修改审批拒绝
                    for (CAcctEntry acctentry : acctEntryListOlds) {
                        acctentry.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                        acctentry.setOprStatus(acctEntryList.get(0).getOprStatus());
                        acctentry.setAuditRefuseReason(acctEntryList.get(0).getAuditRefuseReason());
                        acctentry.setAuditTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                    }
                    this.updateBatchById(acctEntryListOlds);
                    //修改临时表审核状态为修改审批拒绝
                    for (BizTmp biztmp : bizTmpList) {
                        biztmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        biztmp.setLastOprId(user.getId());
                        biztmp.setLastUpdateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        //新增、修改、删除临时表记录
                        BizTmpData(biztmp);
                    }

                }
            }
        }
    }

    /**
     * 保存历史记录
     *
     * @param acctentrylist
     * @param acctentryList
     * @param user
     * @param instId
     * @param oprMenuId
     * @param oprMenuName
     * @param lastUpddt
     */
    private void BizHisData(List<CAcctEntry> acctentrylist, List<CAcctEntry> acctentryList, UpmpUserInfoVO user, String instId, String oprMenuId, String oprMenuName, String lastUpddt) throws RedpException {
        List entitys = new ArrayList<>();
        List entityOlds = new ArrayList<>();
        List bizOpr = new ArrayList<>();
        List realIds = new ArrayList<>();

        if (null != acctentryList) {
            Iterator<CAcctEntry> itMsir = acctentryList.iterator();
            while (itMsir.hasNext()) {
                CAcctEntry msto = (CAcctEntry) itMsir.next();
                int flag = 0;

                Iterator<CAcctEntry> itMsi = acctentrylist.iterator();
                while (itMsi.hasNext()) {
                    CAcctEntry mst = (CAcctEntry) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { // 正式表的数据在临时表中已经不存在
                    /*listDel.add(msto);*/
                    entitys.add(new CInstructionRule());
                    entityOlds.add(msto);
                    bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                    realIds.add(msto.getId());
                }
            }
        }

        if (null != acctentrylist) {//分析出待新增的数据
            Iterator<CAcctEntry> itMsir = acctentrylist.iterator();
            while (itMsir.hasNext()) {
                CAcctEntry mst = (CAcctEntry) itMsir.next();
                int flag = 0;

                Iterator<CAcctEntry> itMso = acctentryList.iterator();
                while (itMso.hasNext()) {
                    CAcctEntry msto = (CAcctEntry) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    entitys.add(mst);
                    entityOlds.add(new CInstructionRule());
                    bizOpr.add(BizOprEnum.OPR_ADD.getType());
                    realIds.add(mst.getId());
                }
            }
        }

        for (int j = 0; j < acctentryList.size(); j++) {
            for (int i = 0; i < acctentrylist.size(); i++) {
                if (acctentrylist.get(i).getId().equals(acctentryList.get(j).getId())) {
                    entitys.add(acctentrylist.get(i));
                    entityOlds.add(acctentryList.get(j));
                    bizOpr.add(BizOprEnum.OPR_UPDATE.getType());
                    realIds.add(acctentryList.get(j).getId());
                }
            }
        }
        bizHisService.saveBizHisList(entitys, entityOlds, user, instId, bizOpr, lastUpddt, oprMenuId, oprMenuName, realIds, null, null, null);
    }

    private void cancelupdate(String acctEntryId, String instId, String type, String oprStatus, String id, String formatDate, String cancelReason) {
    }

    private void useupdate(String acctEntryId, String instId, String type, String oprStatus, String id, String formatDate) {
    }

    /**
     * 停用修改正式表状态
     *
     * @param acctEntryId
     * @param instId
     * @param auditStatus
     * @param oprStatus
     * @param authUid
     * @param lastUpddt
     * @param stopReason
     */
    private void stopupdate(String acctEntryId, String instId, String auditStatus, String oprStatus, String authUid, String lastUpddt, String stopReason) {
        LambdaQueryWrapper<CAcctEntry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CAcctEntry::getAcctEntryId, acctEntryId).eq(CAcctEntry::getInstId, instId);
        List<CAcctEntry> list = baseMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            for (CAcctEntry acctEntry : list) {
                acctEntry.setAuditStatus(auditStatus);
                acctEntry.setOprStatus(oprStatus);
                acctEntry.setOprId(authUid);
                acctEntry.setUpdateTime(lastUpddt);
                acctEntry.setStopReason(stopReason);
                LambdaUpdateWrapper<CAcctEntry> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(CAcctEntry::getAcctEntryId, acctEntryId).eq(CAcctEntry::getInstId, instId);
                baseMapper.update(acctEntry, wrapper);
            }
        }
    }

    /**
     * 当bizData1存放业务表的唯一索引的json串时，调用此方法可以查询该业务数据对应的临时表数据
     *
     * @param instId
     * @param ename
     * @param acctEntryId
     * @return
     */
    private List<BizTmp> getBizTmpListByUniqueKey(String instId, String ename, String acctEntryId) {
        LambdaQueryWrapper<BizTmp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizTmp::getInstId, instId).eq(BizTmp::getOprTableEname, ename).eq(BizTmp::getBizData1, acctEntryId);
        List<BizTmp> list = bizTmpService.list(wrapper);
        if (Common.isNotBlank(list) && list.size() > 0) {
            return list;
        }
        return null;
    }

    private void BizTmpData(BizTmp tmp) {
        BizTmp bizTmp = bizTmpService.getBaseMapper().selectById(tmp);
        if (null == bizTmp) {
            bizTmp = tmp;
            bizTmpService.getBaseMapper().insert(bizTmp);
        } else {
            bizTmpService.getBaseMapper().updateById(tmp);
        }

    }

    private List<CAcctEntry> findacctEntryId(String acctEntryId, String instId) {
        LambdaUpdateWrapper<CAcctEntry> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CAcctEntry::getAcctEntryId, acctEntryId).eq(CAcctEntry::getInstId, instId);
        return baseMapper.selectList(wrapper);
    }

    private void upstatus(String acctEntryId, String type, String oprStatus, String id, String formatDate) {
        LambdaUpdateWrapper<CAcctEntry> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CAcctEntry::getAcctEntryId, acctEntryId).set(CAcctEntry::getAuditStatus, type)
                .set(CAcctEntry::getOprStatus, oprStatus).set(CAcctEntry::getOprId, id)
                .set(CAcctEntry::getUpdateTime, formatDate);
        this.update(wrapper);

//        CAcctEntry acctEntry = baseMapper.selectById(acctEntryId);
//        acctEntry.setAuditStatus(type);
//        acctEntry.setOprStatus(oprStatus);
//        acctEntry.setOprId(id);
//        acctEntry.setUpdateTime(formatDate);
//        acctEntry.setAuditRefuseReason("");
//        baseMapper.updateById(acctEntry);

    }

    /**
     * 系统生成会计分录标识（格式：AE + 6位序号）
     *
     * @return
     */
    private String generateCode() {
        // 固定入参
        String common = "COMM_PARAM_CODE";
        // 会计分录标识前缀：IN
        String prefix = CsmcConstant.AcctEntryId_PREFIX;
        // 生成序列号
        String serialNumber = sysSeqService.updateParamValue(common, prefix, "20");
        // 编码：AE + 6位顺序编号
        String result = prefix + String.format("%06d", Integer.parseInt(serialNumber));
        return result;
    }


    /**
     * 新增、修改、删除正式表明细记录
     *
     * @param
     * @throws RedpException
     */
    public void mergeData(List<CAcctEntry> acctEntryList, String instId) throws RedpException {

        LambdaQueryWrapper<CAcctEntry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CAcctEntry::getAcctEntryId, acctEntryList.get(0).getAcctEntryId());
        List<CAcctEntry> listOrg = baseMapper.selectList(queryWrapper);
        List<CAcctEntry> listDel = new ArrayList<CAcctEntry>();//待删除的数据
        List<CAcctEntry> listNew = new ArrayList<CAcctEntry>();//待新增的数据

        if (null != listOrg) {//分析出待删除的数据
            Iterator<CAcctEntry> itMsir = listOrg.iterator();
            while (itMsir.hasNext()) {
                CAcctEntry msto = (CAcctEntry) itMsir.next();
                int flag = 0;

                Iterator<CAcctEntry> itMsi = acctEntryList.iterator();
                while (itMsi.hasNext()) {
                    CAcctEntry mst = (CAcctEntry) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { //临时表中的数据在此次提交时已经被删除
                    listDel.add(msto);
                }
            }
        }

        if (null != acctEntryList) {//分析出待新增的数据
            Iterator<CAcctEntry> itMsir = acctEntryList.iterator();
            while (itMsir.hasNext()) {
                CAcctEntry mst = (CAcctEntry) itMsir.next();
                int flag = 0;

                Iterator<CAcctEntry> itMso = listOrg.iterator();
                while (itMso.hasNext()) {
                    CAcctEntry msto = (CAcctEntry) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    listNew.add(mst);
                }
            }
        }
        if (Common.isNotBlank(listOrg) && listOrg.size() > 0) {//修改
            this.saveOrUpdateBatch(listOrg);
        }
        if (Common.isNotBlank(listNew) && listNew.size() > 0) {//新增
            for (CAcctEntry acctEntry : listNew) {
                acctEntry.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
            }
            this.saveBatch(listNew);
        }
        if (Common.isNotBlank(listDel) && listDel.size() > 0) {//删除
            for (CAcctEntry acctEntry : listDel) {
                this.removeById(acctEntry.getId());
            }
        }
    }

}
